﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Text;
using OptusMobile.Web.ServerComponents.BAL.HandsetManagement;
using OptusMobile.Web.ServerComponents.BAL.PlanManagement;
using OptusMobile.Web.ServerComponents.DAL;



namespace OptusMobile.Web.Handset
{
    public partial class ShowHandset : System.Web.UI.Page
    {
        private HandsetManager handsetManager;
        
        

        public ShowHandset()
        {
            handsetManager = new HandsetManager();
            HttpCookie cookie = new HttpCookie("recommederImage");
            cookie.Value = "0";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string cssPath = "../css/ShowHandsetDetail.css";

            //AddCss.AddStyleSheet(this.Page, cssPath); 

            HtmlLink link = new HtmlLink();
            link.Href = cssPath;
            link.Attributes["rel"] = "stylesheet";
            link.Attributes["type"] = "text/css";
            PlaceHolder1.Controls.Add(link);
            if (Page.IsPostBack == false)
            {
                Session["handsetImageID"] = Request.QueryString["imageID"].ToString();
                ShowHandsetOtherImage(Request.QueryString["imageID"].ToString());
                Image img = (Image)DetailImagesPHolder.FindControl("handsetimage0");
                ImgMain.ImageUrl = img.ImageUrl;
            }
            if(DetailImagesPHolder.HasControls()==false)
            {
                ShowHandsetOtherImage(Session["handsetImageID"].ToString ());
            }
           ShowRecommderHandset(Request.QueryString["handsetID"].ToString());
        }


        private void OnClick_RecommderImage(Object sender, CommandEventArgs e)
        {
            ShowHandsetOtherImage(e.CommandArgument.ToString ());
            Session["handsetImageID"] = e.CommandArgument.ToString();
            Image img = (ImageButton)DetailImagesPHolder.FindControl("handsetimage0");
            ImgMain.ImageUrl = img.ImageUrl;
            for (int i = 0; i < 10; i++)
            {
                if (e.CommandName.Equals(i.ToString()))
                {
                    Session["detaileCurImage"] = i;
                    break;
                }
            }
            DetailImagesPHolder.FindControl("handsetimage" + Session["detaileCurImage"].ToString()).Focus();
        }


        private void ShowHandsetOtherImage(string ImageID)
        {
            DetailImagesPHolder.Controls.Clear();
            HtmlGenericControl dynamicControl = new HtmlGenericControl();
            var result = handsetManager.GetHandsetDetailImage(ImageID);
            int handsetCount = result.Count;
          
            for (int i = 0; i < handsetCount; i++)
            {
                ImageButton imghandset = new ImageButton();
                imghandset.ID = "handsetimage" + i.ToString();
                imghandset.ImageUrl = ".." + result[i].imagePath + result[i].imageName;
                imghandset.AlternateText = result[i].imageName.ToString();
                imghandset.Command += new CommandEventHandler(this.OnClick_DetailImage);
                
                imghandset.CommandName = i.ToString();
                imghandset.CommandArgument = imghandset.ImageUrl;
                imghandset.EnableViewState = true;
                dynamicControl.Controls.Add(imghandset);
            }

            DetailImagesPHolder.Controls.Add(dynamicControl);
           

        }

        private void OnClick_DetailImage(Object sender, CommandEventArgs e)
        {
           
            ImgMain.ImageUrl = e.CommandArgument.ToString();
            for (int i=0;i<10;i++)
            {
                if (e.CommandName.Equals(i.ToString()))
                {
                    Session["detaileCurImage"] = i;
                    break;
                }
            }
            DetailImagesPHolder.FindControl("handsetimage" + Session["detaileCurImage"].ToString()).Focus();
            ShowHandsetOtherImage(Session["handsetImageID"].ToString ());
        }

        private void ShowRecommderHandset(string HandsetID)
        {
            HtmlGenericControl dynamicControl = new HtmlGenericControl();
            var handset = handsetManager.GetHandsetSimilarityByAttribute().Where(p => p.handsetID == HandsetID && p.similarty > 0.92);

            int i = 0;
            foreach (V_HandsetSimilarity hand in handset)
            {
                var image = handsetManager.GetHandsetImage(hand.campareHandsetID.ToString());
                ImageButton recommdhandset = new ImageButton();
                recommdhandset.ID = "recommderhandset" + i.ToString();
                recommdhandset.ImageUrl = ".." + image.imagePath + image.imageName;
                recommdhandset.Command += new CommandEventHandler(this.OnClick_RecommderImage);
                var imgdetail = handsetManager.GetHandsetImage(hand.campareHandsetID.ToString ());
                recommdhandset.CommandArgument = imgdetail.imageID.ToString();
                recommdhandset.CommandName = "0";
                recommdhandset.EnableViewState = true;
                dynamicControl.Controls.Add(recommdhandset);
                i = i + 1;
            }

            //for (int i = 0; i < handsetCount; i++)
            //{
                
            //    ImageButton recommdhandset = new ImageButton();
            //    recommdhandset.ID = "recommderhandset" + i.ToString();
            //    recommdhandset.ImageUrl = ".." + handsetManager.GetHandsetImage()[i].imagePath + handsetManager.GetHandsetImage()[i].imageName;
            //    recommdhandset.Command += new CommandEventHandler(this.OnClick_Image);
            //    recommdhandset.CommandArgument = recommdhandset.ImageUrl;
                
            //    recommdhandset.EnableViewState = true;
            //    dynamicControl.Controls.Add(recommdhandset);

            //}

            RecommderPHolder.Controls.Add(dynamicControl);
            
            
        }

        protected void btnDtRight_Click(object sender, ImageClickEventArgs e)
        {
            ShowHandsetOtherImage(Session["handsetImageID"].ToString());
            int detaileCurImage = Convert.ToInt32(Session["detaileCurImage"].ToString()) + 1;
            if (detaileCurImage < 10)
            {
                ImageButton b = (ImageButton)DetailImagesPHolder.FindControl("handsetimage" + detaileCurImage.ToString());
                Session["detaileCurImage"] = detaileCurImage;
                ImgMain.ImageUrl = b.ImageUrl;
                
            }
           
        }

        protected void btnDtLeft_Click(object sender, ImageClickEventArgs e)
        {
            ShowHandsetOtherImage(Session["handsetImageID"].ToString());
            int detaileCurImage = Convert.ToInt32(Session["detaileCurImage"].ToString()) - 1;
            if (detaileCurImage > 0)
            {
                ImageButton b = (ImageButton)DetailImagesPHolder.FindControl("handsetimage" + detaileCurImage.ToString());
                Session["detaileCurImage"] = detaileCurImage;
                ImgMain.ImageUrl = b.ImageUrl;

            }
        }

        protected void btnReLeft_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void btnMainLeft_Click(object sender, ImageClickEventArgs e)
        {
            ShowHandsetOtherImage(Session["handsetImageID"].ToString());
            int detaileCurImage = Convert.ToInt32(Session["detaileCurImage"].ToString()) - 1;
            if (detaileCurImage > 0)
            {
                ImageButton b = (ImageButton)DetailImagesPHolder.FindControl("handsetimage" + detaileCurImage.ToString());
                Session["detaileCurImage"] = detaileCurImage;
                ImgMain.ImageUrl = b.ImageUrl;

            }
        }

        protected void btnMainRight_Click(object sender, ImageClickEventArgs e)
        {
            ShowHandsetOtherImage(Session["handsetImageID"].ToString());
            int detaileCurImage = Convert.ToInt32(Session["detaileCurImage"].ToString()) + 1;
            if (detaileCurImage < 10)
            {
                ImageButton b = (ImageButton)DetailImagesPHolder.FindControl("handsetimage" + detaileCurImage.ToString());
                Session["detaileCurImage"] = detaileCurImage;
                ImgMain.ImageUrl = b.ImageUrl;

            }
        }



    }
}
